package com.example.weather_news.fragment.frag_items;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.Response;
import com.example.weather_news.R;
import com.example.weather_news.adapter.Adapter_RecycleNews;
import com.example.weather_news.fragment.BaseFragment;
import com.example.weather_news.http.HttpRequest;
import com.example.weather_news.util.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/11.
 */

public class Frag_News extends BaseFragment {
    private static final String CHANNEL_ID = "channelId";
    private RecyclerView list_news;
    private SwipeRefreshLayout swipeRefresh;

    private Adapter_RecycleNews adapter_recycleNews;

    private JSONArray array;
    private int start=0;

    public static Frag_News newInstance(String channelId) {
        Bundle bundle = new Bundle();
        Frag_News frag_news = new Frag_News();
        bundle.putString(CHANNEL_ID, channelId);
        frag_news.setArguments(bundle);
        return frag_news;
    }

    @Override
    public void initView() {
        list_news = findView(R.id.list_news);
        swipeRefresh = findView(R.id.swipeRefresh);
    }

    @Override
    public void initData() {
        getNews();
        //下拉刷新
        swipeRefresh.setSize(SwipeRefreshLayout.LARGE);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getNews();
            }
        });

        list_news.setHasFixedSize(true);
        LinearLayoutManager manager=new LinearLayoutManager(getActivity());
        list_news.setLayoutManager(manager);
        adapter_recycleNews=new Adapter_RecycleNews(getActivity());
        list_news.setAdapter(adapter_recycleNews);
    }

    private void getNews() {
        if (!swipeRefresh.isRefreshing()) {
            swipeRefresh.setRefreshing(true);
        }

        start= ShareUtils.getInt(getActivity(),ShareUtils.INDEX);

        String channelId = getArguments().getString(CHANNEL_ID);
        Log.e("channelId", channelId);
        HttpRequest.requestNews(channelId,start,new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.getInt("status") != 0) {
                        showToast(jsonObject.getString("msg"));
                    } else {
                        String result = jsonObject.getString("result");
                        JSONObject jo = new JSONObject(result);
                        String list = jo.getString("list");
                        array = new JSONArray(list);

                        if (swipeRefresh.isRefreshing()) {
                            swipeRefresh.setRefreshing(false);
                        }
                        adapter_recycleNews.setData(array);
                        start+=20;
                        ShareUtils.setInt(getActivity(),ShareUtils.INDEX,start);
                        if (start==100){
                            start=0;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);
    }

    @Override
    public int getLayoutId() {
        return R.layout.frag_news;
    }


}
